<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020-8-11
 * Time: 11:59
 */

namespace App\Member\Renderables;

use App\Models\UserBalanceLog;
use App\Models\WeekCheckout;
use App\Models\WeekProfitReturn;
use Illuminate\Contracts\Support\Renderable;
use Dcat\Admin\Admin;
use Carbon\Carbon;


class AdminUserPieRenderables implements Renderable
{

    public static $js = [
        'echarts/echarts/dist/echarts.js',
    ];


    public function __construct()
    {
        $this->user = Admin::user();
    }


    public function teamScript()
    {

        $user          = Admin::user();
        $data          = $this->agent();
        $inferiorAgent = $this->inferiorAgent();

        //dd($data, $inferiorAgent);
        if (count($data) <= 0) {
            $data = collect($data);

            $data->zt_volume = 0;
            $data->yj_volume = 0;
            $data->zt_profit = 0;
            $data->yj_profit = 0;
            $data->tj_profit = 0;
            $data->tj_profit = 0;
        } else {
            $data = $data[0];
        }

        if (count($inferiorAgent) <= 0) {
            $data->now_amount = 0;
        } else {
            $data->now_amount = $inferiorAgent[0]->now_amount;
        }
        $admin_user_ztssylxx = trans('handlers.admin_user_ztssylxx');//.'直推手数结算'},
        $admin_user_tdyjssyl = trans('handlers.admin_user_tdyjssyl');//.'团队业绩手数结算'},
        $admin_user_ztyjfhxx = trans('handlers.admin_user_ztyjfhxx');//.'直推业绩分红'},
        $admin_user_tdyjfhxx = trans('handlers.admin_user_tdyjfhxx');//.'团队业绩分红'},
        $admin_user_tjyjxxxx = trans('handlers.admin_user_tjyjxxxx');//.'同级佣金'},
        $admin_user_xjtxyjxx = trans('handlers.admin_user_xjtxyjxx');//.'下级提现佣金'}

        return <<<JS

            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('pie'));

            // 指定图表的配置项和数据
            option = {

                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b}: {c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    left: 10,
                    top: 20,
                    data: [
                        "$admin_user_ztssylxx",
                        "$admin_user_tdyjssyl",
                        "$admin_user_ztyjfhxx",
                        "$admin_user_tdyjfhxx",
                        "$admin_user_tjyjxxxx",
                        "$admin_user_xjtxyjxx"
                    ]
                },
                series: [
                    {
                        name: '访问来源',
                        type: 'pie',
                        selectedMode: 'single',
                        radius: [0, '30%'],

                        label: {
                            position: 'inner'
                        },
                        labelLine: {
                            show: false
                        }
                    },
                    {
                        name: '',
                        type: 'pie',
                        radius: ['40%', '55%'],
                        label: {
                            formatter: '{a|{a}}{abg|} {b|{b}：}{c} ',
                            backgroundColor: '#eee',
                            borderColor: '#aaa',
                            borderWidth: 1,
                            borderRadius: 4,
                            rich: {
                                a: {
                                    color: '#999',
                                    lineHeight: 22,
                                    align: 'center'
                                },
                                hr: {
                                    borderColor: '#aaa',
                                    width: '100%',
                                    borderWidth: 0.5,
                                    height: 0
                                },
                                b: {
                                    fontSize: 10,
                                    lineHeight: 24
                                },
                                per: {
                                    color: '#eee',
                                    backgroundColor: '#334455',
                                    padding: [2, 4],
                                    borderRadius: 2
                                }
                            }
                        },
                        data: [
                            {value: $data->zt_volume, name: "$admin_user_ztssylxx"}, //.'直推手数结算'},
                            {value: $data->yj_volume, name: "$admin_user_tdyjssyl"}, //.'团队业绩手数结算'},
                            {value: $data->zt_profit, name: "$admin_user_ztyjfhxx"}, //.'直推业绩分红'},
                            {value: $data->yj_profit, name: "$admin_user_tdyjfhxx"}, //.'团队业绩分红'},
                            {value: $data->tj_profit, name: "$admin_user_tjyjxxxx"}, //.'同级佣金'},
                            {value: $data->now_amount, name: "$admin_user_xjtxyjxx"}, //.'下级提现佣金'}
                        ]
                    }
                ]
            }

            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
JS;
    }


    public function render()
    {
        // 在这里可以引入你的js或css文件
        Admin::js(static::$js);

        // 需要在页面执行的JS代码
        // 通过 Admin::script 设置的JS代码会自动在所有JS脚本都加载完毕后执行
        Admin::script($this->teamScript());

        return view('member.admin_user.echarts_pie')->render();
    }


    /***
     * 下级产品
     * @return mixed
     */
    public function agent()
    {
        $agent = WeekProfitReturn::where('user_id', $this->user->id)
            ->where('ismail', 2)
            ->select([
                \DB::raw('SUM(zt_volume) as zt_volume'),
                \DB::raw('SUM(yj_volume) as yj_volume'),
                \DB::raw('SUM(zt_profit) as zt_profit'),
                \DB::raw('SUM(yj_profit) as yj_profit'),
                \DB::raw('SUM(tj_profit) as tj_profit'),
                \DB::raw('user_id as date'),
            ])->groupBy('date')->get();

        return $agent;
    }


    /***
     * 下级提现佣金
     */
    public function inferiorAgent()
    {
        return UserBalanceLog::where('user_id', $this->user->id)
            ->where('amount_type', 2)
            ->where('type', 0)
            //->orderByDesc('created_at')
            ->select([
                \DB::raw('SUM(now_amount) as now_amount'),
                \DB::raw('user_id as user_id'),
            ])->groupBy('user_id')->get();
    }


}
